package triangle.datastructures;

import java.util.*;

public class Tree<T> {

	TreeNode<T> root = null;
	
	public Tree(T elem)
	{
		root = new TreeNode<T>(elem);
		root.parent = null;
	}
	/**
	 * Implements the simplest unbalanced tree method.
	 * Level specifies 
	 * @param elem
	 */
	public void add(T elem, int level)
	{
		
	}
	
	class TreeNode<T>
	{
		// elemenet stored in this node
		public T elem;
		// parent of the node
		public TreeNode<T> parent;
		// children of the node
		public HashMap<T, TreeNode<T>> children = new HashMap<T, TreeNode<T>>();
		
		public TreeNode(T elem)
		{
			this.elem = elem;
		}
		
		public void addChild(T elem)
		{
			TreeNode<T> c = new TreeNode<T>(elem);
			c.parent = this;
			children.put(elem, c);
		}
		
		/**
		 * Still keeps the grandchilren, but removes the child node.
		 * @param elem
		 */
		public void disownChild(T elem)
		{
			if (children.containsKey(elem))
			{
				TreeNode<T> c = children.get(elem);
				HashMap<T, TreeNode<T>> grandChildren = c.children;
				this.children.putAll(grandChildren);
				for (T gc : grandChildren.keySet())
				{
					TreeNode<T> g = grandChildren.get(gc);
					g.parent = this;
				}
				this.children.remove(elem);
			}
		}
		
		/**
		 * remove child, and all its children.
		 * Since this is java, just need to lose child, and all will follow.
		 * @param elem
		 */
		public void abortion(T elem)
		{
			if (children.containsKey(elem))
			{
				children.remove(elem);
			}
		}
		
		public String toString()
		{
			return this.elem.toString();
		}
	}
}
